package cn.hls.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import cn.hls.dao.CertSetDao;
import cn.hls.dao.CompanyDao;
import cn.hls.dao.OrgDao;
import cn.hls.entity.CertSet;
import cn.hls.entity.Company;
import cn.hls.entity.Org;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.CompanyService;

/**
 * @fileName: CompanyDaoImpl.java
 * @package: cn.christ.dao.impl
 * @Description: 会友数据接口实现类
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Component("companyService")
public class CompanyServiceBean extends BaseServiceBean<Company> implements
		CompanyService {
	@Resource(name = "companyDao")
	private CompanyDao dao;
	@Resource(name="certSetDao")
	private CertSetDao certSetDao;
	@Resource(name="orgDao")
	private OrgDao orgDao;
	@Override
	public boolean add(Company entity) throws Exception {
		boolean flag= super.add(entity);
		if(entity.getType().intValue()==1){//承运企业
			if(flag){//添加成功后，再将企业信息添加到组织机构中
				Org org=new Org(entity.getId(),entity.getName(),entity.getContractor(),entity.getContractPhone());
				flag&=orgDao.add(org);
			}
		}
		return flag;
	}
	@Override
	public PageInfo<Company> loadCompanyInfo(Company entity,
			PageInfo<Company> page) throws Exception {
		return dao.loadCompanyInfo(entity, page);
	}
	
	@Override
	public List<Company> loadCompanyList(Company entity)throws Exception{
		return dao.loadCompanyList(entity);
	}

	public List<Company>loadCompanyOfShipper(Company entity)throws Exception{
		return dao.loadCompanyOfShipper(entity);
	}
	@Override
	public boolean exists(Company entity) throws Exception {
		return dao.exists(entity);
	}
	
	@Override
	public void delete(Object entityId) throws Exception {
		dao.delete(entityId);
	}
	
	@Override
	public boolean update(Company entity) throws Exception {
		Company tempCompany=dao.find(entity.getId());
		Date createTime=tempCompany.getCreateTime();
		BeanUtils.copyProperties(entity, tempCompany);
		tempCompany.setCreateTime(createTime);
		tempCompany.setUpdateTime(new Date());
		return super.update(tempCompany);
	}
	
	 
	public Map<String,Integer>loadExpiredCompany(Long companyId)throws Exception{
		return dao.loadExpiredCompany(companyId,certSetDao.loadcertSetInfoToMap(null));
	}

	@Override
	public List<Company> loadExpiredCompanyList(Company entity,
			Map<String, CertSet> cetSetMap) throws Exception {
		return dao.loadExpiredCompanyList(entity, cetSetMap);
	}

}
